package com.example.passport.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.Gateway;
import com.example.passport.dto.RoleSearchDto;
import com.example.passport.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper {

    @Insert("INSERT INTO `role`" +
            " (`name`,`spec`,`sort`,`status`) VALUES" +
            " ( #{name}, #{spec}, #{sort}, #{status})")
    void create(Role role);

    @Update("UPDATE `role` SET " +
            "`name` = #{role.name} " +
            ",`spec` = #{role.spec} " +
            ",`sort` = #{role.sort} " +
            ",`status` = #{role.status} " +
            " WHERE `id`=#{role.id} " +
//            " AND `tenant_id`=#{role.tenantId}" +
            " AND `status`!=" + Role.STATUS_DELETE)
    int update(@Param("role") Role role);

    @Select("<script>" +
            "SELECT * FROM `role`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.status!=0'> AND `status`=#{condition.status} </if>" +
//            " AND `tenant_id`=#{condition.tenantId} " +
            "AND `status`!=" + Role.STATUS_DELETE +
            "</where>" +
            " ORDER BY `created_at` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Role> findAll(@Param("condition") RoleSearchDto condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `role`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.status!=0'> AND `status`=#{condition.status} </if>" +
//            " AND `tenant_id`=#{condition.tenantId} " +
            " AND `status`!=" + Role.STATUS_DELETE +
            "</where>" +
            "</script>")
    long count(@Param("condition") RoleSearchDto condition);

    @Select("SELECT * FROM role WHERE `name` = #{name} " +
//            "AND `tenant_id`=#{tenantId}" +
            " AND status != " + Role.STATUS_DELETE + " LIMIT 1")
    Role findByName(@Param("name") String name, @Param("tenantId") Long tenantId);

    @Select("SELECT * FROM role WHERE " +
//            "`tenant_id` = #{tenantId} AND " +
            "`id` = #{id}  AND `name` = #{name} LIMIT 1")
    Role findByIdName(@Param("tenantId") long tenantId, @Param("id") long id, @Param("name") String name);

    @Select("SELECT * FROM role WHERE " +
//            "`tenant_id` = #{tenantId} AND " +
            "`id` = #{id} LIMIT 1")
    Role findById(@Param("tenantId") long tenantId, @Param("id") long id);

    @Delete("DELETE FROM role WHERE id=#{id} " +
//            "AND tenant_id = #{tenantId}" +
            "")
    int deleteById(long id, long tenantId);
}
